Skip to content

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Jul 24, 2025

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the python implementation, the Binary class has a from_vector static method. You have certainly considered this solution, of having only a factory method instead of a new class. But the new methods toArray and getVectorType are specific to this binary type, so having a dedicated class makes sense. But if you create a new Binary($raw, Binary::TYPE_VECTOR) instance with the vector type from the raw binary data, you don't have this methods. The current state doesn't allow creating a BinaryVector from raw data. I think you would have to wrap in into a BSON Document and encode/decode it to get a BinaryVector instance. I don't find any other binary type that would require a specific class.

I see 3 possible implementations:

  1. Add Binary::fromVector(array $vector, VectorType $vectorType) and toArray() to the existing Binary class
  2. Create BinaryVector class that extends Binary (current state of this PR).
  3. Create BinaryVector class that implements BinaryInterface, \JsonSerializable, Type, \Stringable.

We need to compare pro/con of each solution.

@jmikola jmikola marked this pull request as ready for review October 3, 2025 02:02
@jmikola jmikola requested a review from a team as a code owner October 3, 2025 02:02
@jmikola jmikola requested review from alcaeus and removed request for a team October 3, 2025 02:02
@jmikola
Copy link
Member Author

jmikola commented Oct 3, 2025

One failing test appears to be from Atlas connectivity tests failing to connect to a serverless cluster. Per PHPLIB-1667, I expect that should have been removed; however, I don't see a related PHPC ticket. Please review ignoring that particular test failure and I'll address that with a separate PR and ticket.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No objection to adding corpus tests in a follow-up PR.

Comment on lines 6 to 8
var_dump(MongoDB\BSON\VectorType::Float32);
var_dump(MongoDB\BSON\VectorType::Int8);
var_dump(MongoDB\BSON\VectorType::PackedBit);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get all the cases:

Suggested change
var_dump(MongoDB\BSON\VectorType::Float32);
var_dump(MongoDB\BSON\VectorType::Int8);
var_dump(MongoDB\BSON\VectorType::PackedBit);
var_dump(MongoDB\BSON\VectorType::cases());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume cases() returns everything in the declaration order (from the stub/header), but I'm not sure if that's an implementation detail. A more robust solution might be to construct an ordered list of test cases and then do a separate assertion to ensure it's consistent with what cases() returns.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the documentation:

This method will return a packed array of all cases in an enumeration, in order of declaration.

So you don't have to sort them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var_dump(VectorType::cases());
array(3) {
  [0]=>
  enum(VectorType::Float32)
  [1]=>
  enum(VectorType::Int8)
  [2]=>
  enum(VectorType::PackedBit)
}

--FILE--
<?php

foreach (VectorType::cases() as $case) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: added usage of cases().

@@ -0,0 +1,18 @@
--TEST--
MongoDB\BSON\Binary unserialization requires valid vector data (__serialize and __unserialize)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this complements bson-binary-set_state_error-004.phpt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants